import Vue from 'vue'
import Router from 'vue-router'
import login from '../views/login'
import register from '../views/register/register'
// import manager from '../views/manager/index'  // v1.0 蓝色
//import manager from '../views/manager/black' // v1.1 黑色
import manager from '../views/manager/blue' // v1.2 蓝色

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '',            // 访问的地址
      redirect: '/login',  // 重定向
      component: login     // 组件名称
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/register',
      name: 'register',
      component: register
    },
    {
      path: '/manager',
      name: 'manager',
      redirect: '/system/welcome',
      component: manager,
      children: [
        {
          path: '/system/welcome',
          name: 'welcome',
          component: () => import("../views/system/welcome")
        },
        {
          path: '/system/stuIframe',
          name: 'stuIframe',
          component: () => import("../views/service/stuIframe")
        },
        {
          path: '/system/stuMenu',
          name: 'stuMenu',
          component: () => import("../views/system/stuMenu")
        },
        {
          path: '/system/stuClass',
          name: 'stuClass',
          component: () => import("../views/system/stuClass")
        },
        {
          path: '/system/user',
          name: 'user',
          component: () => import("../views/system/user")
        },
        {
          path: '/service/chat',
          name: 'chat',
          component: () => import("../views/service/chat")
        },
        {
          path: '/service/chatn',
          name: 'chatn',
          component: () => import("../views/chatn")
        },
        {
          path: '/service/socketChat',
          name: 'socketChat',
          component: () => import("../views/service/chat/socketChat")
        },
        {
          path: '/tool/richText',
          name: 'richText',
          component: () => import("../views/tool/richText")
        },
        {
          path: '/tool/drag',
          name: 'drag',
          component: () => import("../views/tool/drag")
        }
      ]
    },
  ]
});

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('Authorization')
    if (token === 'null' || token === '') {
      next('/login');
    } else {
      // 看是否是嵌入页面，嵌入页面url前缀有iframe:
      let path = to.path;
      if (path.indexOf("iframe:") !== -1) {
        // router.push('/system/iframe');
        router.push({path: '/system/stuIframe', query: {url: path}});
      } else {
        next();
      }
    }
  }
});

export default router;
